package com.jt.common.util;

import java.net.URLEncoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {

	public final static String KEY = "~PEGLMf#Kg0z0sCf";
	public static String encryptAES(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
		byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
		return new BASE64Encoder().encode(bytes);

	}

	public static String decryptAES(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
		byte[] bytes = new BASE64Decoder().decodeBuffer(str);
		bytes = cipher.doFinal(bytes);
		return new String(bytes, "utf-8");
	}

	public static String encryptAES(String str) throws Exception {
		return encryptAES(str, KEY);
	}

	public static String decryptAES(String str) throws Exception {
		return decryptAES(str, KEY);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(URLEncoder.encode(encryptAES("18600070286"), "utf-8"));
		
		//"/user/cooperationReg.json?phone="+URLEncoder.encode(encryptAES("18600070286"), "utf-8")+"channelId=YYWCQ";
	}

}
